<template>
  <el-dialog title="收货结算更新" :close-on-click-modal="false" :visible.sync="visible" center fullscreen>
    <el-button class="miui-dialog-close-btn" @click="visible = false">关闭页面</el-button>
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" label-width="120px">
      <el-row>
        <el-col :span="8">
          <el-form-item label="单据编号" prop="billId">
            <el-input v-model="dataForm.billId"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="订单编号">
            <el-input readonly></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="单据日期">
            <el-input v-model="dataForm.createTime"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="8">
          <el-form-item label="所属制造商">
            <el-input v-model="dataForm.supplyName"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="入库类型">
            <el-input value="采购入库"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="接收仓位">
            <el-input v-model="dataForm.inStockName"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="8">
          <el-form-item label="制单人">
            <el-input v-model="dataForm.createUserName"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="入库审核人">
            <el-input v-model="dataForm.auditUserName"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="运单号">
            <el-input></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="备注" prop="memo">
            <el-input type="textarea" :rows="2" placeholder="请输入内容" v-model="dataForm.memo"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="计算方式">
            <el-radio v-model="calculate" label="1" @change="changeCalculate">自动计算</el-radio>
            <el-radio v-model="calculate" label="2" @change="changeCalculate">手动计算</el-radio>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-button type="primary" size="mini" @click="dataFormSubmit" :loading="ischeck" v-prevent-re-click>保存</el-button>
          <el-switch v-model="dataForm.financeStatus" active-text="保存并审核" inactive-value=0 active-value=1 style="margin-left: 15px;"></el-switch>
        </el-col>
      </el-row>
    </el-form>
    <el-table :data="dataForm.detailList" border v-loading="dataListLoading" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55"></el-table-column>
      <el-table-column prop="goodsCode" label="商品代码"></el-table-column>
      <el-table-column prop="goodsName" label="商品名称"></el-table-column>
      <el-table-column prop="spec" label="型号"> </el-table-column>
      <el-table-column prop="price" label="零售价"></el-table-column>
      <el-table-column prop="quantity" label="采购数量"></el-table-column>
      <el-table-column prop="notTaxPrice" label="单位成本">
        <template slot-scope="scope">
          <el-input v-model="scope.row.notTaxPrice" :disabled="isDisabled"></el-input>
        </template>
      </el-table-column>
      <el-table-column prop="totalNotTaxPrice" label="成本合计">
        <template slot-scope="scope">
          <el-input v-model="scope.row.totalNotTaxPrice" :disabled="isDisabled"></el-input>
        </template>
      </el-table-column>
      <el-table-column prop="taxRate" label="税率">
        <template slot-scope="scope">
          <el-input v-model="scope.row.taxRate" @change="changeTaxRate(scope.row,scope.$index)"></el-input>
        </template>
      </el-table-column>
      <el-table-column prop="taxPrice" label="含税单价">
        <template slot-scope="scope">
          <el-input v-model="scope.row.taxPrice" @change="changeTaxPrice(scope.row)"></el-input>
        </template>
      </el-table-column>
      <el-table-column prop="taxRatePrice" label="税额合计">
        <template slot-scope="scope">
          <el-input v-model="scope.row.taxRatePrice" :disabled="isDisabled"></el-input>
        </template>
      </el-table-column>
      <el-table-column prop="totalTaxPrice" label="价税合计">
        <template slot-scope="scope">
          <el-input v-model="scope.row.totalTaxPrice" :disabled="isDisabled"></el-input>
        </template>
      </el-table-column>
    </el-table>
    <span slot="footer" class="dialog-footer">
      <el-button type="primary" @click="dataFormSubmit" :loading="ischeck" v-prevent-re-click>保存</el-button>
      <el-switch v-model="dataForm.financeStatus" active-text="保存并审核" inactive-value=0 active-value=1 style="margin-left: 15px;"></el-switch>
    </span>
  </el-dialog>
</template>

<script>
    import {receiveApplyGetObj, receiveApplyPutObj} from '@/api/eims/purchase/receive-apply'
    import { receiveGoodsGetObj } from '@/api/eims/purchase/receive-goods'
    import { getFinanceInfoById,updateFinanceByIdPutObj } from '@/api/eims/purchase/receive-settlement'
    import { fetchCtgList } from '@/api/eims/ctg'
    export default {
    data () {
      return {
				ischeck: false,
        visible: false,
        calculate: '1',
        dataForm: {
          id: 0,
          ctgId: '',
          ctgName: '',
          supplyId: '',
          supplyName: '',
          memo: '',
          financeStatus: 0,
          detailList: []
        },
        ctgOptions: [],
        dataList: [],
        dataListLoading: false,
        dataRule: {ctgId: [{ required: true,message: '请选择采购类别'}],supplyName: [{required:true,message: '请选择制造商'}]},
        supplySearchVisible: false,
        goodsListVisible: false,
        selectionList: [],
        isDisabled: true,
      }
    },
    methods: {
      init (id) {
        this.getCtgList()
        this.dataForm.id = id || 0
        this.dataForm.supplyId = ''
        this.visible = true
        this.dataList = []
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.dataListLoading = true
            getFinanceInfoById(this.dataForm.id).then(response => {
              if(response.data.code == '0'){
                this.dataForm = response.data.data
                this.dataForm.detailList.forEach(result => {
                  result.totalNotTaxPrice = (result.quantity * result.notTaxPrice).toFixed(2)
                  result.totalTaxPrice = (result.quantity * result.taxPrice).toFixed(2)
                })
              }else{
                this.$message.error(response.data.msg)
              }
              this.dataListLoading = false
            })
          }
        })
      },
      // 商品类别改变时获取名字
      ctgSelectChange ($event) {
        this.ctgOptions.forEach(item => {
          if(item.value == $event){ this.dataForm.ctgName = item.name }
        })
      },
      // 打开制造商列表弹窗
      checkSupplyList () {
        this.supplySearchVisible = true
        this.$nextTick(() => {
          this.$refs.supplySearch.init(this.dataForm.supplyId)
        })
      },
      // 选择制造商
      renewFormData (data) {
        this.dataForm.supplyId = data.id
        this.dataForm.supplyName = data.name
      },
      // 打开商品列表弹窗
      addGoodsBySingle() {
        // 判断是否选中采购类型
        if(!this.dataForm.ctgId){
          this.$message.warning('请选择采购类型')
          return
        }
        // 判断是否选中制造商
        if(!this.dataForm.supplyId){
          this.$message.warning('请选择制造商')
          return
        }
        let params = {ctgId:this.dataForm.ctgId,supplyId:this.dataForm.supplyId}
        let data = {params: params,data: this.dataList}
        this.goodsListVisible = true
        this.$nextTick(() => {  this.$refs.sinleGoodsList.init(data) })
      },
      handleSelectionChange(val) {
        this.selectionList = val
      },
      // 删除选中的商品
      delTableData() {
        let data = this.selectionList
        data.forEach(item => { this.dataList.splice(item,1) })
      },
      //更新表单数据
      updateFormData(data) {
        this.dataList = data
        let list = []
        data.forEach(item => {
          list.push({goodsId: item.id})
        })
        this.dataForm.detailList = list
      },
      // 表单提交
      dataFormSubmit () {
				this.ischeck = true
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            updateFinanceByIdPutObj(this.dataForm).then(response => {
              if(response.data.code == '0'){
                (this.dataForm.financeStatus == "1") ? this.$message.success('修改并审核成功') : this.$message.success('修改成功')
                this.visible = false
                this.$emit('refreshDataList')
              }else{
                this.$message.error(response.data.msg)
              }
              this.ischeck = false
            }).catch(() => {
							this.ischeck = false
						})
          }else{
						this.ischeck = false
					}
        })
      },
      /*
      * *  获取采购类型列表
      */
     getCtgList(){
       fetchCtgList().then(response => {
         if(response.data.code == '0'){
           let dataList = response.data.data.records
           let list = []
           dataList.forEach(item => {
             list.push({value:item.id,name:item.name})
           })
           this.ctgOptions = list
         }else{
           this.$message.error(response.data.msg)
         }
       })
     },
      //修改计算方式
      changeCalculate(){
        if(this.calculate == '1'){
          this.isDisabled = true
        }else{
          this.isDisabled = false
        }
      },
      changeTaxRate(data,index){
        data.notTaxPrice = (data.taxPrice/(1+data.taxRate/100)).toFixed(6)
        data.totalNotTaxPrice = (data.quantity * data.notTaxPrice).toFixed(2)
        data.taxRatePrice = (data.taxPrice/(1+data.taxRate/100) * (data.taxRate/100) *data.quantity).toFixed(2)
        data.totalTaxPrice = (data.quantity * data.taxPrice).toFixed(2)
      },
      changeTaxPrice(data) {
        data.notTaxPrice = (data.taxPrice/(1+data.taxRate/100)).toFixed(6)
        data.totalNotTaxPrice = (data.quantity * data.notTaxPrice).toFixed(2)
        data.taxRatePrice = (data.taxPrice/(1+data.taxRate/100) * (data.taxRate/100) *data.quantity).toFixed(2)
        data.totalTaxPrice = (data.quantity * data.taxPrice).toFixed(2)
      },
    }
  }
</script>
